import MusicConfig from "../../engine/config/MusicConfig";
import CommonTips from "../../engine/uicomponent/CommonTips";
import EventManager from "../../engine/utils/EventManager";
import HotUpdate from "../../engine/utils/HotUpdate";
import { Logger } from "../../engine/utils/Logger";
import VersionManager from "../../engine/utils/VersionManager";
import ResourcePreload from "../utils/ResourcePreload";
import SceneBase from "./SceneBase";
import SceneManager from "./SceneManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class LoadingScene extends SceneBase{

    public static scriptName: string = "LoadingScene";

    // @property({ type: cc.Node })
    // private progressNode: cc.Node = null;


    onLoadMe(){
        this.baseInit();
        EventManager.instance.addListener(HotUpdate.Event_On_NeedUpdate, this.onNeedUpdate, this);
        EventManager.instance.addListener(HotUpdate.Event_On_Progress, this.onUpdateProgress, this);
        EventManager.instance.addListener(HotUpdate.Event_On_Fail_Update, this.onUpdateFail, this);
        EventManager.instance.addListener(HotUpdate.Event_Finish_Update, this.onUpdateFinish, this);
        EventManager.instance.addListener(HotUpdate.Event_On_ALREADY_UP_TO_DATE, this.onUpdateFinish, this);
        this.refreshVersion(false);
        if (cc.sys.isNative && VersionManager.instance.isOpenHotUpdate) {
            this.checkUpdate();
        }
        else {
            this.preLoadRes();
        }
    }

    private baseInit(){
        MusicConfig.init();
        // cc.director.getCollisionManager().enabled=true;//这是一个全局属性，开启后就代表碰撞检测组件可以进行检测了
        // cc.director.getCollisionManager().enabledDebugDraw = true; //绘制碰撞区域
    }

    private checkUpdate() {
        Logger.log("checkUpdate====");
        VersionManager.instance.checkUpdate(0);
    }

    private onNeedUpdate(key: string) {
        Logger.log("onNeedUpdate=====", key, VersionManager.Config_Key);
        if (key == VersionManager.Config_Key[0]) {
            VersionManager.instance.startUpdate(0);
        }
    }

    private onUpdateProgress(loadedFiles, totalFiles, key) {
        if (key == VersionManager.Config_Key[0]) {
            let msg: string = Math.min(100, (loadedFiles / totalFiles * 100)).toFixed(2) + "%";
            // this.progressNode.getComponent(Progress).updateProgress(loadedFiles, totalFiles, msg);
            this.refreshVersion(true);
        }
    }

    private refreshVersion(showTargetVersion: boolean) {
        // this.txtVersion.string = "当前版本V" + VersionManager.instance.nowVersion;
        // if (showTargetVersion) {
        //     this.txtTargetVersion.string = "目标版本V" + VersionManager.instance.targetVersion;
        // } else {
        //     this.txtTargetVersion.string = ""
        // }
    }

    private onUpdateFail(key: string) {
        if (key == VersionManager.Config_Key[0]) {
            Logger.warn("热更新失败========");
            CommonTips.showMsg("热更新失败")
            ResourcePreload.instance.restartGame();
        }
    }

    private onUpdateFinish(key: string, needRestart: boolean) {
        Logger.log("onUpdateFinish========");
        this.refreshVersion(false);
        if (key == VersionManager.Config_Key[0] && !needRestart) {
            this.preLoadRes();
        }
    }

    private async preLoadRes() {
        ResourcePreload.instance.preLoad(() => {
            this.startGame();
        });
        // }, this.progressNode.getComponent(Progress));
    }

    private startGame(){
        Logger.info("startGame")

        SceneManager.instance.sceneSwitch("RhythmScene");
        // SceneManager.instance.sceneSwitch(BookScene.scriptName);
    }

    onDestroyMe() {
        EventManager.instance.removeListener(HotUpdate.Event_On_NeedUpdate, this.onNeedUpdate);
        EventManager.instance.removeListener(HotUpdate.Event_On_Progress, this.onUpdateProgress);
        EventManager.instance.removeListener(HotUpdate.Event_On_Fail_Update, this.onUpdateFail);
        EventManager.instance.removeListener(HotUpdate.Event_Finish_Update, this.onUpdateFinish);
        EventManager.instance.removeListener(HotUpdate.Event_On_ALREADY_UP_TO_DATE, this.onUpdateFinish);
    }


}
